#!/bin/bash

#set -x

##  Arguments:
##  stop  - stops cimom and kills processes
##  start - starts HSC by calling startup scripts
##  start debug - set debug log flag, start rmiregistry, switch sniacimom.jar.

function checkVersion {
   typeset -i pv1
   typeset -i pv2
   typeset -i pr
   typeset -i v1
   typeset -i v2
   typeset -i r
   pv1=0
   pv2=0
   pr=0
   r=`rpm -q -i IBMhsc.coreserver|grep Release|cut -d ':' -f 2 | cut -d ' ' -f 2`
   v1=`rpm -q -i IBMhsc.coreserver|grep Version|cut -d ':' -f 2 | cut -d ' ' -f 2 | cut -d '.' -f 1`
   v2=`rpm -q -i IBMhsc.coreserver|grep Version|cut -d ':' -f 2 | cut -d ' ' -f 2 | cut -d '.' -f 2`
   if [[ $pv1 -eq 0  ||  $v1 -gt $pv1 ]]
   then
        pv1=v1
   fi
   if [[ $pv2 -eq 0  ||  $v2 -gt $pv2 ]]
   then
        pv2=v2
   fi 
   if [[ $pr -eq 0  ||  $r -gt $pr ]]
   then
        pr=r
   fi 
   echo " Release: "$pr
   echo " Version: "$pv1"."$pv2
}

if [ "$2" = "" ]
then
  FLAG=false
else
  FLAG=$2
fi
CIMOMID=/var/hsc/sniacimom.pid

JAVAPATH="/opt/IBMJava2-131/jre/bin/"
x=`type -p java 2>/dev/null`
if [ "$x" != "" ]
then
  JAVAPATH=`/usr/bin/dirname $x`
fi
OLDPWD=`pwd`
if [ "${DEBUG_JARS_DIRECTORY}" != "" ] ; then
  if [ -d ${DEBUG_JARS_DIRECTORY} ] ; then
    for i in ${DEBUG_JARS_DIRECTORY}/*.jar
    do
       debug_jars=${debug_jars}:$i
    done
  fi
fi

CLASSPATH=${DEBUG_JARS_DIRECTORY}:${debug_jars}:/usr/websm/codebase/wsm.jar:/usr/websm/codebase/pluginjars/sniacimom.jar:/usr/websm/codebase/pluginjars/xerces.jar:/usr/websm/codebase/pluginjars/HwmcaCommon.jar:/usr/websm/codebase/pluginjars/auifw.jar:$CLASSPATH
export CLASSPATH
PATH=$JAVAPATH:/opt/hsc/bin:$PATH
export PATH
case $1 in
stop )

  #Stop cimom

  /opt/hsc/bin/initCIMOM stop

  #Stop everything else
 
  for i in CIMClient WConsole ServerT VtermServerStart lced 
  do

    STOP_PID=`ps -efww | grep $i | grep -v grep | cut -c10-16`
    echo $STOP_PID
    if [ "$STOP_PID" != "" ]
    then
        echo Stopping $i
	if [ "$i" = "lced" ]
        then
          kill -15 $STOP_PID > /dev/null 2>&1
	else
	  kill -9 $STOP_PID > /dev/null 2>&1
        fi

        # wait (with timeout) 
        for j in 1 2 3 4 5 6
        do
                if ps -efww | grep $i > /dev/null 2>&1 
                then
                        sleep 1
                else
                        break
                fi
        done
    fi

  done

;;

start )

#   Need to switch between the debug/no-debug environment
#
  JAR_PATH="/usr/websm/codebase/pluginjars"
  if [ "$FLAG" = "debug" -a ! -f /var/hsc/log/.DEBUG_HSC ]; then
    touch /var/hsc/log/.DEBUG_HSC
    /opt/hsc/bin/cfgManager start 

    diff ${JAR_PATH}/sniacimom.jar ${JAR_PATH}/sniacimom.jar.db > /dev/null
    ret_val=$?
    if [ $ret_val -eq 1 ]; then
      mv -f ${JAR_PATH}/sniacimom.jar ${JAR_PATH}/sniacimom.jar.Hsc_ori
      cp -p ${JAR_PATH}/sniacimom.jar.db ${JAR_PATH}/sniacimom.jar
    elif [ $ret_val -gt 1 ]; then
      echo " !!!"
      echo " ERROR: file ${JAR_PATH}/sniacimom.jar.db "
      echo "          or ${JAR_PATH}/sniacimom.jar is missing !"
    fi
  elif [ "$FLAG" = "false" -a -f /var/hsc/log/.DEBUG_HSC ]; then
	rm -rf /var/hsc/log/.DEBUG_HSC > /dev/null
    /opt/hsc/bin/cfgManager stop

    diff ${JAR_PATH}/sniacimom.jar ${JAR_PATH}/sniacimom.jar.db > /dev/null
    ret_val=$?
    if [ $ret_val -eq 0 ]; then
      if [ -f ${JAR_PATH}/sniacimom.jar.Hsc_ori ]; then
        cp -p ${JAR_PATH}/sniacimom.jar.Hsc_ori ${JAR_PATH}/sniacimom.jar
      else
        echo " !!!"
        echo " WARNING: the ${JAR_PATH}/sniacimom.jar.Hsc_ori file is missing !"
      fi
    fi
  fi

  export LD_LIBRARY_PATH=/usr/lib/:/opt/hsc/lib/:/opt/hsc/lib/hmcjni/:$LD_LIBRARY_PATH
  echo start HSC GUI
  xterm -e /opt/hsc/bin/startHSC &
;;

version )
  checkVersion
  cat /opt/hsc/data/version
;;

"" )
 echo usage: hsc stop  - stops cimom and processes 
 echo        hsc start - executes startup scripts
 echo        hsc start debug - enable HSC debug mode.
;;

esac
 

cd $OLDPWD
